package com.hele.sts.dao;

import com.hele.sts.pub.ConnectDB;
import com.hele.sts.pub.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class XtUserLoginDao {

    private static final Logger logger = LoggerFactory.getLogger(XtUserLoginDao.class);

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public XtUserLogin queryByUsername(String username){

            String sql = "select * from XT_USER_LOGIN where username = ?";
//            String sql = "select username,last_login_time as lastLoginTime,login_error_count as loginErrorCount,login_state as loginSate from XT_USER_LOGIN where username = ?";
//            List<XtUserLogin> list = jdbcTemplate.queryForList(sql, new Object[]{username}, new RowMapper<XtUserLogin>() {
//                @Override
//                public XtUserLogin mapRow(ResultSet rs, int rowNum) throws SQLException {
//                    XtUserLogin userLogin = new XtUserLogin();
//                    return userLogin;
//                }
//            });
            logger.debug("queryByUsername: {},{}",sql,username);
            XtUserLogin xtUser;
            try {
                xtUser = jdbcTemplate.queryForObject(sql, new Object[]{username}, new RowMapper<XtUserLogin>() {
                    @Override
                    public XtUserLogin mapRow(ResultSet rs, int rowNum) throws SQLException {
                        XtUserLogin userLogin = new XtUserLogin();
                        userLogin.setUsername(rs.getString(1));
                        userLogin.setLastLoginTime(rs.getTimestamp(2));
                        userLogin.setLoginErrorCount(rs.getInt(3));
                        userLogin.setLoginState(rs.getInt(4));
                        userLogin.setAllowLoginTime(rs.getTimestamp(5));

                        return userLogin;
                    }
                });
                return xtUser;
            }catch (EmptyResultDataAccessException e){
                return null;
            }catch (DataAccessException e){
                logger.error("queryByUsername error",e);
                return null;
            }

//            if(list!=null&&list.size()>0){
//                return list.get(0);
//            }else {
//                return null;
//            }
            //return this.jdbcTemplate.queryForObject(sql, new Object[]{username},XtUserLogin.class);
       }

    public boolean insert(XtUserLogin bean){
        Connection conn = null;
        PreparedStatement pstmt = null;
        String sql = null;
        ConnectDB connsyb = null;
        sql = "INSERT INTO XT_USER_LOGIN VALUES (?,?,?,?,?)";
        logger.debug(sql);
        try {
            this.jdbcTemplate.update(sql,bean.getUsername(),bean.getLastLoginTime(),bean.getLoginErrorCount(),bean.getLoginState(),bean.getAllowLoginTime());
            return true;
        } catch (DataAccessException e) {
            logger.info("XtUserLoginDao insert", e);
            return false;
        } finally {

        }
    }

    public boolean update(XtUserLogin bean) {
        String sql = "UPDATE XT_USER_LOGIN SET last_login_time=?, login_error_count=?,login_state=?,allow_login_time=? where username=?";
        logger.debug(sql);
        try {
            this.jdbcTemplate.update(sql,bean.getLastLoginTime(),bean.getLoginErrorCount(),bean.getLoginState(),bean.getAllowLoginTime(),bean.getUsername());
            return true;
        } catch (DataAccessException e) {
            logger.info("XtUserLoginDao update", e);
            return false;
        } finally {

        }
    }
}
